package cn.doitedu.demo8.web_test;

import org.junit.Test;
import org.roaringbitmap.longlong.Roaring64Bitmap;

import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class RuleInjectWebTest {

    @Test
    public void ruleMode1() throws Exception {

        Connection connection = DriverManager.getConnection("jdbc:mysql://doitedu:3306/doit41", "root", "root");
        PreparedStatement stmt = connection.prepareStatement("insert into rule_meta values(?,?,?,?,?)");
        stmt.setString(1, "rule-1-001");
        stmt.setString(2, "model-1");
        stmt.setString(3, "{\"rule_id\":\"rule-1-001\",\"event_id\":\"G\"}");
        stmt.setInt(4, 1);

        // 去es中根据上面的规则参数中的静态画像条件查询满足的人群
        Roaring64Bitmap bitmap = Roaring64Bitmap.bitmapOf(1, 3, 4, 5, 7, 8);
        // 将bitmap序列化
        ByteArrayOutputStream bout = new ByteArrayOutputStream();
        DataOutputStream dout = new DataOutputStream(bout);
        bitmap.serialize(dout);
        byte[] bitMapBytes = bout.toByteArray();

        stmt.setBytes(5, bitMapBytes);

        // 执行insert语句
        stmt.execute();

    }


    @Test
    public void ruleMode2() throws Exception {

        Connection connection = DriverManager.getConnection("jdbc:mysql://doitedu:3306/doit41", "root", "root");
        PreparedStatement stmt = connection.prepareStatement("insert into rule_meta values(?,?,?,?,?)");
        stmt.setString(1, "rule-2-001");
        stmt.setString(2, "model-2");
        stmt.setString(3, "{\"rule_id\":\"rule-2-001\",\"profile_conditions\":[{\"tag_name\":\"gender\",\"compare_type\":\"=\",\"condition_values\":[\"female\"]}],\"realtime_profile\":{\"event_id\":\"P\",\"min_count\":2},\"fire_condition\":{\"event_id\":\"G\"}}");
        stmt.setInt(4, 1);

        // 去es中根据上面的规则参数中的静态画像条件查询满足的人群
        Roaring64Bitmap bitmap = Roaring64Bitmap.bitmapOf(10, 13, 24, 55, 67, 86, 100);
        // 将bitmap序列化
        ByteArrayOutputStream bout = new ByteArrayOutputStream();
        DataOutputStream dout = new DataOutputStream(bout);
        bitmap.serialize(dout);
        byte[] bitMapBytes = bout.toByteArray();

        stmt.setBytes(5, bitMapBytes);

        // 执行insert语句
        stmt.execute();

    }


}
